package com.soar.tbox.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.jasypt.util.binary.BasicBinaryEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;


/**
 * 加密解密辅助类
 * @author songjunjie
 *
 */
public class EncryptHelper {
	/**
	 * 给指令加密
	 * @param command
	 * @return
	 * @throws IOException 
	 */
	public static byte[] encrypt(byte[] data) throws IOException{
		BasicBinaryEncryptor  bbe = new BasicBinaryEncryptor();
		String password = SettingHelper.getSetting().get("password");
		bbe.setPassword(password);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		out.write(data, 1, data.length-1);
		byte[] body = out.toByteArray();
		try{
			byte[] newbody = bbe.encrypt(body);
			byte[] newdata = Converts.asArray(new byte[]{data[0]},newbody);
			return newdata;
		}catch(RuntimeException e){
			e.printStackTrace();
			throw e;
		}
		
		
		
	}
	
	public static byte[] decrypt(byte[] data){
		BasicBinaryEncryptor  bbe = new BasicBinaryEncryptor();
		String password = SettingHelper.getSetting().get("password");
		bbe.setPassword(password);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		out.write(data, 1, data.length-1);
		byte[] body = out.toByteArray();
		byte[] newbody = bbe.decrypt(body);
		byte[] newdata = Converts.asArray(new byte[]{data[0]},newbody);
		return newdata;
	}
	
	/**
	 * 给的加密的短信指令字符串解密
	 * @param body
	 * @return
	 */
	public static String decryptSmsBody(String body){
		String password = SettingHelper.getSetting().get("password");
		BasicTextEncryptor bte = new BasicTextEncryptor();
		bte.setPassword(password);
		return bte.decrypt(body);
		
	}
}
